package com.example.firstjavamqtt.controller.base_api;

import com.example.firstjavamqtt.service.base.DeviceService;
import com.example.firstjavamqtt.common.result.Result;
import com.example.firstjavamqtt.model.base.Device;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/base/device")
@Api(tags = "设备接口")
public class DeviceController {

    @Autowired
    DeviceService deviceService;

    @ApiOperation("获取设备列表")
    @GetMapping("/getDeviceList")
    public Result getDeviceList() {
        return Result.ok(deviceService.getDeviceList());
    }

    @ApiOperation("分页获取设备列表")
    @GetMapping("/getDevicePageList/{pageNum}/{limit}")
    public Result getDevicePageList(@PathVariable int pageNum,
                                    @PathVariable int limit) {
        return Result.ok(deviceService.getDevicePageList(pageNum,limit));
    }

    @ApiOperation("根据设备类型删除设备")
    @GetMapping("/deleteByDeviceType/{deviceType}")
    public Result deleteDeviceByDeviceType(@PathVariable String deviceType) {
        deviceService.deleteDeviceByDeviceType(deviceType);
        return Result.ok();
    }

    @ApiOperation("根据设备ID删除设备")
    @GetMapping("/deleteByDeviceId/{deviceId}")
    public Result deleteDeviceByDeviceId(@PathVariable @ApiParam(value = "设备ID") String deviceId) {
        deviceService.deleteDeviceByDeviceId(deviceId);
        return Result.ok();
    }

    @ApiOperation("根据ID删除设备")
    @GetMapping("/deleteById/{id}")
    public Result deleteDeviceById(@PathVariable int id) {
        //我其实觉得可以抛异常?
        deviceService.deleteDeviceById(id);
        return Result.ok();
    }

    @ApiOperation("根据批量ID删除设备")
    @GetMapping("/deleteByBatchId/{batchId}")
    public Result deleteDeviceByBatchId(@PathVariable ArrayList<Integer> batchId) {
        deviceService.deleteDeviceByBatchId(batchId);
        return Result.ok();
    }

    @ApiOperation("设置设备上线下线")
    @PostMapping("/setDeviceStatus/{deviceId}/{online}")
    public Result setDeviceStatus(@PathVariable @ApiParam(value = "设备ID") String deviceId, @PathVariable @ApiParam(value = "是否在线") Boolean online) {
        deviceService.setDeviceStatus(deviceId, online);
        return Result.ok();
    }

    @ApiOperation("根据deviceId获取设备")
    @GetMapping("getDeviceByDeviceId/{deviceId}")
    public Result getDeviceByDeviceId(@PathVariable @ApiParam(value = "设备ID") String deviceId) {
        Device device = deviceService.getDeviceByDeviceId(deviceId);
        return Result.ok(device);
    }

    @ApiOperation("根据deviceType获取设备")
    @GetMapping("getDeviceByDeviceType/{deviceType}")
    public Result getDeviceByDeviceType(@PathVariable @ApiParam(value = "设备类型") String deviceType) {
        List<Device> deviceList = deviceService.getDeviceByDeviceType(deviceType);
        return Result.ok(deviceList);
    }

}
